﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace TotalFreedomKO
{
    public class KoElement3HtmlString<TModel, TProperty> : KoAttributeHtmlString<TModel, TProperty>, IKoElementHtmlString<TModel>
    {
        private KoAttributeHtmlString<TModel, TProperty> _elementKoAttribute;

        private readonly string _startValue;
        private readonly string _middleValue;
        private readonly string _endValue;
        private readonly string[] _supportedAttributes;
        private readonly string[] _firstSectionAttributes;

        public KoElement3HtmlString(string startValue, string middleValue, string endValue, string[] firstSectionAttributes, string attributeName, string attributeValue, ViewDataDictionary<TModel> viewData = null, string[] supportedAttributes = null)
            : base(null, attributeName, attributeValue, viewData, supportedAttributes)
        {
            _startValue = startValue;
            _middleValue = middleValue;
            _endValue = endValue;
            _supportedAttributes = supportedAttributes;
            _firstSectionAttributes = firstSectionAttributes;

            if (!String.IsNullOrEmpty(attributeName))
                _elementKoAttribute = new KoAttributeHtmlString<TModel, TProperty>(null, attributeName, attributeValue, viewData, supportedAttributes);
        }

        public override string ToHtmlString()
        {
            return _startValue + _middleValue + (_elementKoAttribute != null ? _elementKoAttribute.ToHtmlString() : "") + _endValue;
        }

        public string ToHtmlString(IKoAttributeHtmlString<TModel> koAttribute)
        {
            string fd = "";
            string firstAttributeValues = "";

            string sd = "";
            string secondAttributeValues = "";

            if (koAttribute != null)
            {
                List<KeyValuePair<string, string>> values = koAttribute.GetAttributeValues();
                if (values != null)
                {
                    foreach (KeyValuePair<string, string> value in values)
                    {
                        string attributeName = Regex.Replace(value.Value, ":.*$", "").Trim();
                        if (_firstSectionAttributes != null && _firstSectionAttributes.Contains(attributeName))
                        {
                            firstAttributeValues += fd + value.Value;
                            fd = ", ";
                        }
                        else
                        {
                            secondAttributeValues += sd + value.Value;
                            sd = ", ";
                        }
                    }
                }
            }

            if (_elementKoAttribute != null)
            {
                string attributeName = Regex.Replace(_elementKoAttribute._attributeValue, ":.*$", "").Trim();
                if (_firstSectionAttributes != null && _firstSectionAttributes.Contains(attributeName))
                    firstAttributeValues += fd + _elementKoAttribute._attributeValue;
                else
                    secondAttributeValues += sd + _elementKoAttribute._attributeValue;

            }

            return _startValue + (!String.IsNullOrEmpty(firstAttributeValues) ? " data-bind=\"" + firstAttributeValues + "\"" : "") + _middleValue + (!String.IsNullOrEmpty(secondAttributeValues) ? " data-bind=\"" + secondAttributeValues + "\"" : "") + _endValue;
        }

        public override bool IsKoAttributeSupported(string attributeName)
        {
            if (_elementKoAttribute != null && !_elementKoAttribute.IsKoAttributeSupported(attributeName))
                return false;

            if (_supportedAttributes != null && !_supportedAttributes.Contains(attributeName))
                return false;

            return true;
        }

        public override bool HasKoElement()
        {
            return true;
        }

        public override IKoElementHtmlString<TModel> GetKoElement()
        {
            return this;
        }

        public override bool HasKoAttribute(string attributeName)
        {
            if (_elementKoAttribute != null)
                return _elementKoAttribute.HasKoAttribute(attributeName);

            return false;
        }
    }
}